package cn.hnu.qq.client.panel;

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import cn.hnu.qq.client.controller.LogonController;
import cn.hnu.qq.entity.User;
import cn.hnu.qq.frame.ClientFrame;
import cn.hnu.qq.frame.RegisterDialog;

/**
 * 登陆面板
 * @author jared
 *
 */
public class LogonPanel extends JPanel implements ActionListener,MouseListener{
	private JTextField textField;
	private JPasswordField passwordField;
	private JCheckBox pwdBox;
	private JCheckBox accountBox;
	private JComboBox comboBox;
	private JLabel registLabel;
	
	private LogonController controller = LogonController.getInstance();
	/**
	 * Create the panel.
	 */
	
	public LogonPanel() {
		init();	
	}
	
	private void init() {
		setLayout(null);
		setSize(290, 242);
		JLabel acountLabel = new JLabel("acount:");
		acountLabel.setBounds(67, 22, 54, 15);
		add(acountLabel);
		
		textField = new JTextField();
		textField.setBounds(131, 19, 96, 21);
		add(textField);
		textField.setColumns(15);
		
		JLabel pwdLabel = new JLabel("password:");
		pwdLabel.setBounds(67, 74, 54, 15);
		add(pwdLabel);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(131, 70, 96, 21);
		add(passwordField);
		
		pwdBox = new JCheckBox("rem-pwd");
		pwdBox.setBounds(173, 111, 133, 23);
		add(pwdBox);
		
		comboBox = new JComboBox();
		comboBox.setBounds(131, 140, 32, 21);
		add(comboBox);
		
		JLabel lblStatus = new JLabel("status:");
		lblStatus.setBounds(67, 140, 54, 15);
		add(lblStatus);
		
		accountBox = new JCheckBox("rem_acount");
		accountBox.setBounds(67, 111, 103, 23);
		add(accountBox);
		
		JButton logonButton = new JButton("logon");
		logonButton.setBounds(46, 171, 93, 23);
		add(logonButton);
		logonButton.addActionListener(this);
		
		JButton concelButton = new JButton("concel");
		concelButton.setBounds(160, 171, 93, 23);
		add(concelButton);
		
		registLabel = new JLabel("no account?regist!!");
		registLabel.setBounds(89, 49, 118, 15);
		add(registLabel);
		registLabel.addMouseListener(this);
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("logon")){
			handleLogon();
		}	
	}

	private void handleLogon() {
		User user = new User();
		user.setAcount(textField.getText());
		user.setPassword(new String(passwordField.getPassword()));
		controller.logon(user);
	}

	public LogonController getController() {
		return controller;
	}

	public void setController(LogonController controller) {
		this.controller = controller;
	}

	public void mouseClicked(MouseEvent e) {
		if(e.getSource() == (registLabel)){
			new RegisterDialog(controller.getClient(), true);
		}
		
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
		if(e.getSource() == (registLabel)){
			registLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		}
	}

	public void mouseExited(MouseEvent e) {
	}
	
	
}
